package com.guiji.file.controller;

import com.guiji.common.annotation.Log;
import com.guiji.common.annotation.RepeatSubmit;
import com.guiji.common.core.controller.BaseController;
import com.guiji.common.core.domain.AjaxResult;
import com.guiji.common.core.page.TableDataInfo;
import com.guiji.common.enums.BusinessType;
import com.guiji.common.utils.poi.ExcelUtil;
import com.guiji.file.bo.ResourceEditBo;
import com.guiji.file.bo.ResourceQueryBo;
import com.guiji.file.service.IResourceService;
import com.guiji.file.vo.ResourceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 文件资源Controller
 *
 * @author 钟维澎（四月）
 * @date 2021-06-29
 */
@Api(value = "文件资源控制器", tags = {"文件资源管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/file/resource")
public class ResourceController extends BaseController {

	private final IResourceService iResourceService;

	/**
	 * 查询文件资源列表
	 */
	@ApiOperation("查询文件资源列表")
	@PreAuthorize("@ss.hasPermi('file:resource:list')")
	@GetMapping("/list")
	public TableDataInfo<ResourceVo> list(@Validated ResourceQueryBo bo) {
		return iResourceService.queryPageList(bo);
	}

	/**
	 * 导出文件资源列表
	 */
	@ApiOperation("导出文件资源列表")
	@PreAuthorize("@ss.hasPermi('file:resource:export')")
	@Log(title = "文件资源", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<ResourceVo> export(@Validated ResourceQueryBo bo) {
		List<ResourceVo> list = iResourceService.queryList(bo);
		ExcelUtil<ResourceVo> util = new ExcelUtil<ResourceVo>(ResourceVo.class);
		return util.exportExcel(list, "文件资源");
	}

	/**
	 * 获取文件资源详细信息
	 */
	@ApiOperation("获取文件资源详细信息")
	@PreAuthorize("@ss.hasPermi('file:resource:query')")
	@GetMapping("/{id}")
	public AjaxResult<ResourceVo> getInfo(@NotNull(message = "主键不能为空")
										  @PathVariable("id") Long id) {
		return AjaxResult.success(iResourceService.queryById(id));
	}

	/**
	 * 文件资源上传
	 */
	@ApiOperation("新增文件资源")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "file", value = "文件", dataType = "java.io.File", required = true),
	})
	@PreAuthorize("@ss.hasPermi('file:resource:add')")
	@Log(title = "文件资源", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@RequestPart("file") MultipartFile file){
		return toAjax(iResourceService.insertByAddBo(file) ? 1 : 0);
	}

	/**
	 * 修改文件资源
	 */
	@ApiOperation("修改文件资源")
	@PreAuthorize("@ss.hasPermi('file:resource:edit')")
	@Log(title = "文件资源", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping
	public AjaxResult<Void> edit(@Validated @RequestBody ResourceEditBo bo) {
		return toAjax(iResourceService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除文件资源
	 */
	@ApiOperation("删除文件资源")
	@PreAuthorize("@ss.hasPermi('file:resource:remove')")
	@Log(title = "文件资源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Long[] ids) {
		return toAjax(iResourceService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}

	@ApiOperation("文件下载")
	@PreAuthorize("@ss.hasPermi('file:resource:query')")
	@Log(title = "文件资源", businessType = BusinessType.EXPORT)
	@GetMapping("/download")
	public void downloadFile(HttpServletResponse response , @RequestParam("id") Long id) {
		iResourceService.download(response, id);
	}

	/**
	 * 查询尚未关联任务的resource详情
	 */
	@ApiOperation("查询尚未关联任务的资源详情")
	public AjaxResult<Void> notStartedList(){
		return null;
	}
}
